﻿using System;
using System.Web.Security;
using JCTimes.DomainModel.Entity;

namespace JCTimes.Portal.Infrastructure
{
    public static class CM
    {
        public const string DEFAULT_PASSWORD = "123456";
        /// <summary>
        /// Format the password via hash algorithm.
        /// </summary>
        /// <param name="password">password to be formatted</param>
        /// <returns>Return a hash password</returns>
        public static string GetMd5Password(string password)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");
        }

        //return the default hash password for user
        public static string GenDefaultPassword()
        {
            return GetMd5Password(DEFAULT_PASSWORD);
        }

        /// <summary>
        /// Format timespan in h时m分.
        /// </summary>
        /// <param name="timespan">Timespan in minutes.</param>
        /// <returns></returns>
        public static string TimeSpanStr(int timespan)
        {
            int min = timespan % 60;
            int hour = timespan / 60;
            return string.Format("{0}时{1}分", hour.ToString(), min < 10 ? "0" + min.ToString() : min.ToString());
        }


        /// <summary> 
        /// 全角转半角 
        /// </summary> 
        /// <param name="source"></param> 
        /// <returns></returns> 
        public static string FullToHalf(string source)
        {
            return ToDBC(source).ToUpper();
        }

        /**/
        /// <summary>
        /// 转半角的函数(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格为12288，半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        ///</remarks>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }




    }
}